/****************************************************************************
 Copyright (c) 2012-2012 lijiejun
 Copyright (c) 2010-2012 cocos2d-x.org
 Copyright (c) 2008-2010 Ricardo Quesada
 Copyright (c) 2011      Zynga Inc.
 
 http://www.cocos2d-x.org
 
 Permission is hereby granted, free of charge, to any person obtaining a copy
 of this software and associated documentation files (the "Software"), to deal
 in the Software without restriction, including without limitation the rights
 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 copies of the Software, and to permit persons to whom the Software is
 furnished to do so, subject to the following conditions:
 
 The above copyright notice and this permission notice shall be included in
 all copies or substantial portions of the Software.
 
 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 THE SOFTWARE.
 ****************************************************************************/

package org.cocos2d.cocoa
{
	import flash.utils.ByteArray;
	
	import org.cocos2d.support.data_support.ccArray;
	
	public class CCArray extends CCObject
	{	
		[Deprecated]
		public static function array() : CCArray
		{
			return CCArray.create();
		}
		
		[Deprecated]
		public static function arrayWithObject(pObject : CCObject) : CCArray
		{
			return CCArray.createWithObject(pObject);
		}
		
		[Deprecated]
		public static function arrayWithObjects(... objects) : CCArray
		{
			var pArray : CCArray = create();
			
			for (var i : uint = 0; i < objects.length; i++)
			{
				if (objects[i] instanceof CCObject)
				{
					pArray.addObject(objects[i]);
				}
			}
			
			return pArray;
		}
		
		[Deprecated]
		public static function arrayWithCapacity(capacity : uint)  : CCArray
		{
			return CCArray.create(capacity);
		}
		
		[Deprecated]
		public static function arrayWithArray(otherArray : CCArray) : CCArray
		{
			return CCArray.create(otherArray);
		}
		
		[Deprecated]
		public static function arrayWithContentsOfFile(data : String) : CCArray
		{
			return CCArray.createWithContentsOfFile(data);
		}
		
		[Deprecated]
		public static function arrayWithContentsOfFileThreadSafe(data : String) : CCArray
		{
			return CCArray.createWithContentsOfFileThreadSafe(data);
		}
		
		public static function create() : CCArray
		{
			var pArray : CCArray = new CCArray();
			
			if (pArray)
				pArray.init();
			
			return pArray;
		}
		
		public static function createWithObject(pObject : CCObject) : CCArray
		{
			var pArray : CCArray = new CCArray();
			
			if (pArray)
				pArray.initWithObject(pObject);
			
			return pArray;
		}
		
		public static function create(... objects) : CCArray
		{
			var pArray : CCArray = create();
			
			for (var i : uint = 0; i < objects.length; i++)
			{
				if (objects[i] instanceof CCObject)
				{
					pArray.addObject(objects[i]);
				}
			}
			
			return pArray;
		}
		
		public static function create(capacity : uint) : CCArray
		{
			var pArray : CCArray = new CCArray();
			
			if (pArray)
				pArray.initWithCapacity(capacity);
			
			return pArray;
		}
		
		public static function create(otherArray : CCArray) : CCArray
		{
			var pRet : CCArray = otherArray.copy();
			return pRet;
		}
		
		public static function createWithContentsOfFile(contents : String) : CCArray
		{
			var pRet : CCArray = createWithContentsOfFileThreadSafe(contents);
			
			return pRet;
		}
		
		public static function createWithContentsOfFileThreadSafe(contents : String) : CCArray
		{
			// TODO
			return null;
		}
		
		public function init() : Boolean
		{
			return initWithCapacity(1);
		}
		
		public function initWithObject(pObject : CCObject) : Boolean
		{
			ccArray.ccArrayFree(data);
			var bRet : Boolean = initWithCapacity(1);
			if (bRet)
			{
				addObject(pObject);
			}
			return bRet;
		}
		
		public function initWithObjects(... objects) : Boolean
		{
			ccArray.ccArrayFree(data);
			var bRet : Boolean = false;
			
			initWithCapacity(objects.length);
			
			for (var i : uint = 0; i < objects.length; i++)
			{
				if (objects[i] instanceof CCObject)
				{	
					addObject(objects[i]);
					bRet = true;
				}
			}
			
			return bRet;
		}
		
		public function initWithCapacity(capacity : uint) : Boolean
		{
			ccArray.ccArrayFree(data);
			data = ccArray.ccArrayNew(capacity);
			return true;
		}
		
		public function initWithArray(otherArray : CCArray) : Boolean
		{
			ccArray.ccArrayFree(data);
			var bRet : Boolean = false;
			if (!initWithCapacity(otherArray.data.num))
				return false;
			addObjectsFromArray(otherArray);
			bRet = true;
			
			return bRet;
		}
		
		
		
		public var data : ccArray;
		
		
		public function CCArray()
		{
		}
	}
}